/*
 *  Copyright © 2011 by Rob Cash
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * DefaultBookmarkServiceDescriptor.java
 *
 * Created on Jun 21, 2011 at 7:27:11 PM
 */
package org.robcash.apps.bookmarksync.provider.spi;

import java.util.ArrayList;
import java.util.List;

import org.robcash.apps.bookmarksync.provider.BookmarkServiceDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Default implementation of a {@link BookmarkServiceDescriptor}.
 * @author Rob Cash
 */
public class DefaultBookmarkServiceDescriptor
        implements BookmarkServiceDescriptor
{

    private static final Logger LOG = LoggerFactory.getLogger(DefaultBookmarkServiceDescriptor.class);

    private String serviceName;
    private boolean loginRequired;
    private boolean readOnly;
    private boolean stateful;
    private List<FeatureSupport> featureSupport;

    /**
     * {@inheritDoc}
     */
    @Override
    public String getServiceName()
    {
        return serviceName;
    }

    /**
     * Set the service name
     * @param name Service name
     */
    public void setServiceName(final String name)
    {
        this.serviceName = name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isLoginRequired()
    {
        return loginRequired;
    }

    /**
     * Set the login required indicator
     * @param value Login required indicator
     */
    public void setLoginRequired(final boolean value)
    {
        this.loginRequired = value;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isReadOnly()
    {
        return readOnly;
    }

    /**
     * Set the read-only indicator
     * @param value Read only indicator
     */
    public void setReadOnly(final boolean value)
    {
        this.readOnly = value;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isStateful()
    {
        return stateful;
    }

    /**
     * Sets the stateful indicator
     * @param value Stateful indicator
     */
    public void setStateful(final boolean value)
    {
        this.stateful = value;
    }

    /**
     * Get the features supported by the service provider
     * @return List of features. This will never be null.
     */
    public List<FeatureSupport> getFeatureSupport()
    {
        if (featureSupport == null)
        {
            featureSupport = new ArrayList<FeatureSupport>();
        }
        return this.featureSupport;
    }

    /**
     * Set the features supported by the service provider
     * @param features List of features supported by the provider
     */
    public void setFeatureSupport(final List<FeatureSupport> features)
    {
        featureSupport = features;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean supportsFeature(final String featureId)
    {
        boolean supported = false;
        try
        {
            FeatureName featureName = FeatureName.fromValue(featureId);
            for( FeatureSupport feature : getFeatureSupport() )
            {
                if ( feature.getName().equals(featureName) )
                {
                    supported = feature.isEnabled();
                    break;
                }
            }
        }
        catch(IllegalArgumentException e)
        {
            // Invalid feature
            LOG.warn("Invalid feature '" + featureId + "'");
        }

        return supported;
    }

}
